﻿@{
    Layout = null;
}
@using PermissionManager.Model;
@{
    var modules = ViewBag.Modules as List<Module>;
    var Funs = ViewBag.Funs as List<MenuExtend>;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="~/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/css/style.css" />
    <link href="~/assets/css/codemirror.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/assets/css/ace.min.css" />
    <link rel="stylesheet" href="~/font/css/font-awesome.min.css" />
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <!--[if lte IE 8]>
      <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
    <![endif]-->
    <script src="~/js/jquery-1.9.1.min.js"></script>
    <script src="~/assets/js/bootstrap.min.js"></script>
    <script src="~/assets/js/typeahead-bs2.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="~/assets/layer/layer.js" type="text/javascript"></script>
    <script src="~/assets/laydate/laydate.js" type="text/javascript"></script>
    <script src="~/js/dragDivResize.js" type="text/javascript"></script>
    <script src="~/layui/layui.js"></script>
    <title>添加权限</title>
</head>

<body>
    <div class="Competence_add_style clearfix">
        <div class="left_Competence_add">
            <div class="title_name">添加权限</div>
            <div class="Competence_add">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限名称 </label>
                    <div class="col-sm-9"><input type="text" id="RoleName" placeholder="" name="权限名称" class="col-xs-10 col-sm-5" /></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1">所属组</label>
                    <div class="col-sm-9" style="margin-left:10px">
                        <select id="RoleModule">
                            @foreach (var item in modules)
                            {
                                <option value=@item.Id>@item.Name</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限描述 </label>
                    <div class="col-sm-9"><textarea name="权限描述" class="form-control" id="RoleRe" placeholder="" onkeyup="checkLength(this);"></textarea><span class="wordage">剩余字数：<span id="sy" style="color:Red;">200</span>字</span></div>
                </div>
                <!--按钮操作-->
                <div class="Button_operation">
                    <button id="article_save_submit" class="btn btn-primary radius" type="submit"><i class="fa fa-save "></i> 保存并提交</button>
                    <button onclick="article_save();" class="btn btn-secondary  btn-warning" type="button"><i class="fa fa-reply"></i> 返回上一步</button>
                    <button onclick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </div>
        <!--权限分配-->
        <div class="Assign_style">
            <div class="title_name">权限分配</div>
            <div class="Select_Competence">
                <dl class="permission-list">
                    @foreach (var item in Funs.Where(a => a.ParentId == 0).OrderBy(a => a.SortId))
                    {
                        var MId = 0;
                        <dt><label class="middle"><input name="@item.Id" class="ace" type="checkbox" id="id-disable-check" /><span class="lbl">@item.Name</span></label></dt>
                        <dd>
                            @foreach (var item1 in Funs.Where(a => a.ParentId == item.Id).OrderBy(a => a.SortId))
                            {
                                if (MId != item1.Id)//判断是否重复
                                {
                                    <dl class="cl permission-list2">
                                        <dt><label class="middle"><input type="checkbox" value="" class="ace" name="@item1.Id" id="id-disable-check" /><span class="lbl">@item1.Name</span></label></dt>
                                        <dd>
                                            @foreach (var item2 in Funs.Where(a => a.Id == item1.Id))
                                            {
                                                <label class="middle"><input type="checkbox" value="" class="ace" name="@item2.FunId" id="user-Character-0-0-0" /><span class="lbl">@item2.FunName</span></label>

                                            }
                                        </dd>

                                    </dl>
                                    MId = item1.Id;
                                }
                            }
                        </dd>
                    }
                </dl>
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    //初始化宽度、高度
    $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();
    $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
    $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
    //当文档窗口发生改变时 触发
    $(window).resize(function () {

        $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
        $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
        $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();;
    });
    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if (which.value.length > maxChars) {
            layer.open({
                icon: 2,
                title: '提示框',
                content: '您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0, maxChars);
            return false;
        } else {
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };
    /*按钮选择*/
    $(function () {
        $(".permission-list dt input:checkbox").click(function () {
            $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function () {
            var l = $(this).parent().parent().find("input:checked").length;
            var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if ($(this).prop("checked")) {
                $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
            }
            else {
                if (l == 0) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                }
                if (l2 == 0) {
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                }
            }
        });
    });
    layui.use(['table', 'form'], function () {
        var table = layui.table,
            form = layui.form;
        /*保存提交*/
        $("#article_save_submit").click(function () {
            var RoleName = $("#RoleName").val();
            var RoleModule = $("#RoleModule").val();
            var RoleRe = $("#RoleRe").val();

            var menuId = 0;//存菜单容器
            var fun = [];//存功能容器
            var FunId = {};
            var MenuFun = {};
            var AllMenuFun = [];//菜单功能集合对象

            if (RoleName != "") {
                /*得到对应的功能*/
                $(".permission-list dt input:checked").each(function () {
                    menuId = $(this).prop("name");
                    $(this).parents(".permission-list2").find("dd input:checked").each(function () {
                        FunId = {
                            Id: $(this).prop("name")
                        }
                        fun.push(
                            FunId
                        )
                    })

                    if ($(this).parents(".permission-list2").find("dd input:checked").length > 0) {
                        MenuFun = {
                            MenuId: menuId,
                            Funs: fun
                        }
                        fun = [];//清空
                        AllMenuFun.push(
                            MenuFun
                        )
                    }
                });
                $.ajax({
                    url: "/Role/AddRole",
                    data: {
                        Name: RoleName,
                        Remark: RoleRe, ModuleId: RoleModule,
                        IsEnable: true,
                        MenuFun: JSON.stringify(AllMenuFun)
                    },
                    type: "post",
                    success: function (data) {
                        if (data.Success) {
                            layer.alert('新增成功！', {
                                title: '提示框',
                                icon: 6,
                            });
                        } else {
                            layer.alert('新增失败！', {
                                title: '提示框',
                                icon: 5,
                            });
                        }
                    }
                });
            } else {
                layer.alert("权限名称不能为空");
            }
        })


    })
</script>